/**
 * @tag controllers, home
 * Displays a table of <%= plural %>.	 Lets the user 
 * ["<%=name.replace("Models","Controllers")%>.prototype.form submit" create], 
 * ["<%=name.replace("Models","Controllers")%>.prototype.&#46;edit click" edit],
 * or ["<%=name.replace("Models","Controllers")%>.prototype.&#46;destroy click" destroy] <%= plural %>.
 */
$.Controller.extend('<%=name.replace("Models","Controllers")%>',
/* @Static */
{
	onDocument: true
},
/* @Prototype */
{
 /**
 * When the page loads, gets all <%= plural %> to be displayed.
 */
 load: function(){
	if(!$("#<%= underscore %>").length){
	 $(document.body).append($('<div/>').attr('id','<%= underscore %>'));
		 <%= name %>.findAll({}, this.callback('list'));
 	}
 },
 /**
 * Displays a list of <%= plural %> and the submit form.
 * @param {Array} <%= plural %> An array of <%=name%> objects.
 */
 list: function( <%= plural %> ){
	$('#<%= underscore %>').html(this.view('init', {<%= plural %>:<%= plural %>} ));
 },
 /**
 * Responds to the create form being submitted by creating a new <%=name%>.
 * @param {jQuery} el A jQuery wrapped element.
 * @param {Event} ev A jQuery event whose default action is prevented.
 */
'form submit': function( el, ev ){
	ev.preventDefault();
	new <%= name %>(el.formParams()).save();
},
/**
 * Listens for <%= plural %> being created.	 When a <%= underscore %> is created, displays the new <%= underscore %>.
 * @param {String} called The open ajax event that was called.
 * @param {Event} <%= underscore %> The new <%= underscore %>.
 */
'<%= underscore %>.created subscribe': function( called, <%= underscore %> ){
	$("#<%= underscore %> tbody").append( this.view("list", {<%= plural %>:[<%= underscore %>]}) );
	$("#<%= underscore %> form input[type!=submit]").val(""); //clear old vals
},
 /**
 * Creates and places the edit interface.
 * @param {jQuery} el The <%= underscore %>'s edit link element.
 */
'.edit click': function( el ){
	var <%= underscore %> = el.closest('.<%= underscore %>').model();
	<%= underscore %>.elements().html(this.view('edit', <%= underscore %>));
},
 /**
 * Removes the edit interface.
 * @param {jQuery} el The <%= underscore %>'s cancel link element.
 */
'.cancel click': function( el ){
	this.show(el.closest('.<%= underscore %>').model());
},
 /**
 * Updates the <%= underscore %> from the edit values.
 */
'.update click': function( el ){
	var $<%= underscore %> = el.closest('.<%= underscore %>'); 
	$<%= underscore %>.model().update($<%= underscore %>.formParams());
},
 /**
 * Listens for updated <%= plural %>.	 When a <%= underscore %> is updated, 
 * update's its display.
 */
'<%= underscore %>.updated subscribe': function( called, <%= underscore %> ){
	this.show(<%= underscore %>);
},
 /**
 * Shows a <%= underscore %>'s information.
 */
show: function( <%= underscore %> ){
	<%= underscore %>.elements().html(this.view('show',<%= underscore %>));
},
 /**
 *	 Handle's clicking on a <%= underscore %>'s destroy link.
 */
'.destroy click': function( el ){
	if(confirm("Are you sure you want to destroy?")){
		el.closest('.<%= underscore %>').model().destroy();
	}
 },
 /**
 *	 Listens for <%= plural %> being destroyed and removes them from being displayed.
 */
"<%= underscore %>.destroyed subscribe": function(called, <%= underscore %>){
	<%= underscore %>.elements().remove();	 //removes ALL elements
 }
});